package com.course.server.service;

import cn.hutool.core.util.StrUtil;
import com.course.server.dto.PageDto;
import com.course.server.dto.RoleDto;
import com.course.server.entity.RoleExample;
import com.course.server.entity.RoleResource;
import com.course.server.entity.RoleResourceExample;
import com.course.server.mapper.RoleMapper;
import com.course.server.mapper.RoleResourceMapper;
import com.course.server.util.CopyUtil;
import com.course.server.util.UuidUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: Durian
 * @date: 2021/6/6 14:35
 */
@Service
public class RoleService {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleResourceMapper roleResourceMapper;

    public PageDto<RoleDto> list(int page, int size) {
        PageHelper.startPage(page, size);
        RoleExample example = new RoleExample();
        List<com.course.server.entity.Role> roles = roleMapper.selectByExample(example);
        PageInfo<com.course.server.entity.Role> pageInfo = new PageInfo<>(roles);

        List<RoleDto> roleDtos = CopyUtil.copyList(roles, RoleDto.class);
        PageDto<RoleDto> pageDto = new PageDto<>();
        pageDto.setPage(page);
        pageDto.setSize(size);
        pageDto.setList(roleDtos);
        pageDto.setTotal(pageInfo.getTotal());
        return pageDto;
    }

    public void save(RoleDto roleDto) {
        com.course.server.entity.Role role = new com.course.server.entity.Role();
        BeanUtils.copyProperties(roleDto, role);
        if (StrUtil.isBlank(roleDto.getId())) {
            insert(role);
        } else {
            update(role);
        }
    }

    public void delete(String id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    private void insert(com.course.server.entity.Role role) {
        role.setId(UuidUtils.getShortUuid());
        roleMapper.insert(role);
    }

    private void update(com.course.server.entity.Role role) {
        roleMapper.updateByPrimaryKeySelective(role);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveResources(RoleDto roleDto) {
        String roleId = roleDto.getId();
        List<String> resourceIds = roleDto.getResourceIds();
        RoleResourceExample example = new RoleResourceExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        // 先删除中间表的对于roleId记录
        roleResourceMapper.deleteByExample(example);
        // 保存角色资源
        resourceIds.forEach(id -> {
            RoleResource roleResource = new RoleResource();
            roleResource.setId(UuidUtils.getShortUuid());
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(id);
            // todo 优化为批量插入
            roleResourceMapper.insert(roleResource);
        });
    }

    public List<String> getResources(String roleId) {
        RoleResourceExample example = new RoleResourceExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        List<RoleResource> roleResources = roleResourceMapper.selectByExample(example);
        return roleResources.stream().map(RoleResource::getResourceId).collect(Collectors.toList());
    }

}
